package com.example.oracle.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oracle.entity.ContainerEntity;
import com.example.oracle.mapper.ContainerMapper;
import com.example.oracle.service.IContainerService;


/**
 * @description: 实现类
 * @author: ydf
 * @date: 2020/12/20 23:54
 * @version: v1.0
 */
@Service
public class ContainerServiceImpl extends ServiceImpl<ContainerMapper, ContainerEntity> implements IContainerService {

    @Resource
    private ContainerMapper userMapper;
    
    @Autowired
    private DataSource dataSource;

    @Override
    public ContainerEntity getContainerById(Long containerId) {

        return userMapper.getContainerById(containerId);
    }

	@Override
	public List<ContainerEntity> getContainerList() {
		List<ContainerEntity> list = null; // 开始时间 
		long stime = System.currentTimeMillis();
		list = userMapper.getContainerList(); // 结束时间
		long etime = System.currentTimeMillis(); // 计算执行时间
		System.out.printf("执行时长：%d 毫秒.", (etime - stime));
		return list;
	}

    @Override
    /*
	 * 查询数据 str为数据库对应的列名，sql为传入的SQL语句
	 * 
	 * @返回一个数据列表
	 */
	public List<Map<String, Object>> getContainerList1() {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = null;
		ArrayList list = null;
		try {
			//从数据库中查询User_ID值为user_id的记录
			sql = "SELECT * FROM CM_CONTAINERS WHERE  rownum < 5000 and PARTFLAG = 'CY'";
			//创建一个连接
			conn = dataSource.getConnection();
			//创建Statement的对象
			stmt = conn.createStatement();
			list = new ArrayList();
			//执行操作，返回一个结果集
			rs = stmt.executeQuery(sql);
			//从结果集中循环读取记录，并且将对应的属性值赋值给user
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				ResultSetMetaData rsmd = rs.getMetaData();
		        for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
		            //String columnName = rsmd.getColumnName(i).toLowerCase();
		            map.put(rsmd.getColumnName(i), rs.getObject(i));		            
		        }
		        //System.out.println(map);
				//实例化UserBean的一个对象
				
				//将user添加到list中
				list.add(map);
			}
			//关闭相关的连接
			rs.close();
			stmt.close();
			//捕获异常
		} catch (SQLException se) {
			se.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//返回list对象
		return list;
	}

    


}
